<?php
// 主要负责事件描述提交
class event extends SDO_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model("events");
	}

	/**
	 * 追加描述
	 */
	public function appendDesc()
	{
		$desc = trim(strip_tags($this->getpara('appendDesc')));
		$compartLine = "\r---------------".date('Y-m-d H:i:s')."追加---------------\r";
		$eventId	= intval($this->getpara('eventId'));
		$username = 'test';
	    if (!$eventId || !$desc) {
	        redirect('incident/NewRequestByCustomer', 'refresh');
	    }
	    $event_desc = $compartLine.$desc;
	    $this->load->model('Events');
	    $row		= array(
			'staff_name'			=> $_SESSION['username'],
    	    'event_desc'			=> $event_desc
	    );

	    if(!$this->Events->appendDescById($eventId, $row)){
	        echo $this->Events->getErrNo . "||" . $this->Events->getErrMessage();
	        exit;
	        redirect('incident/ViewRequestByCustomer/eventId/'.$eventId, 'refresh');
	    }
	    $this->Events->addWorkRecord($eventId, $username, 'XXX追加了描述');
		echo json_encode(array('status'=>'success','content'=>$event_desc));
	}

	/**
	 * 事件处理
	 */
	public function updateEvents()
	{
		$eventId	= $this->getpara("eventId");
		$eventId	= intval($eventId);
		$return = array("success"=>true, "msg"=>"");
		if ($eventId<=0)
		{
			$this->_populateResult("不存在的事件ID", false);
		}
		$event_solution			= $this->getpara("event_solution");
		$operate_content		= $this->getpara("operate_content");
		$processed				= $this->getpara("processed");
		$ret['event_groupid']	= $this->getpara("event_groupid", 1);
		$ret['event_charger']	= $this->getpara("event_charger", "admin");
		$ret['event_priority']	= $this->getpara("event_priority");
		$raw_record				= $this->getpara("raw_record");
		if ($processed) {
			$ret['event_status'] = events_config::$event_status['EVENT_STATUS_SOLUTION'];
		}
		$row		= $this->events->getEventById($eventId);
		if (!$row)
		{
			$this->_populateResult("不存在的事件ID", false);
		}
		if ($row['event_status'] == events_config::$event_status['EVENT_STATUS_SOLUTION'] && $processed)
		{
			$this->_populateResult("事件已经完成，请不要重复完成", false);
		} elseif ($row['event_status'] == events_config::$event_status['EVENT_STATUS_CLOSE']) {
			$this->_populateResult("事件已关闭，无法提交", false);
		}
		$this->load->library("libevent");
		$this->load->library("libworkrecord");

		$arr['event_id']		= $eventId;
		$arr['staff_name']		= 'test';
		$arr['operate_date']	= date('Y-m-d H:i:s');
		if (!$event_solution && $processed) {
			$this->_populateResult("必须提交解决方案，才能完成事件", false);
		}
		if (!empty($event_solution) && !empty($operate_content))
		{
			$ret['event_solution']	= $event_solution;
			$ret['event_charger']	= "admin";
			/**
			 * 追加工作说明
			 */
			$this->libevent->appendEventRecord($eventId, array('event_records' => $operate_content, 'username' => "admin", 'raw_record'=>$raw_record));
			/**
			 * 事件解决方案(不为空时，则event_charger=$_SESSION['username']
			 */
			if ($this->libevent->updateEvent($eventId, $ret)) {
				$arr['operate_content']	= "提交了解决方案:\n".$event_solution."\n-----\n";
				$this->libworkrecord->appendRecord($arr);
				$return['msg'] = "成功提交解决方案";
				if ($row['event_solution']) {
					$return['msg'] = "成功补充了解决方案";
				}
			} else {
				$this->_populateResult("提交失败，请稍后再试", false);
			}
		} else if (!empty($event_solution)) {
			$ret['event_solution']	= $event_solution;
			if ($this->libevent->updateEvent($eventId, $ret)) {
				$arr['operate_content']	= "添加了解决方案:\n".$event_solution;
				$this->libworkrecord->appendRecord($arr);
				$return['msg'] = "成功提交解决方案";
				if ($row['event_solution']) {
					$return['msg'] = "成功补充了解决方案";
				}

			} else {
				$this->_populateResult("提交失败，请稍后再试", false);
			}
		} else if (!empty($operate_content)) {
			if ($this->libevent->appendEventRecord($eventId, array('event_records' => $operate_content, 'username' => "admin", 'raw_record'=>$raw_record))) {
				$arr['operate_content']		= "提交了工作说明:\n".$operate_content;
				$this->libworkrecord->appendRecord($arr);
				$return['msg'] = "工作说明提交成功";
				$this->_populateResult($return);
			} else {
				$this->_populateResult("提交失败，请稍后再试", false);
			}
		} else {
			$this->_populateResult("工作说明和解决方案不能全部为空", false);
		}
		if ($processed) {
			$return['msg'] = "成功提交解决方案，事件已完成";
			$this->_populateResult($return);
		}
		$this->_populateResult($return);
	}

	private function _populateResult($msg, $code=null)
	{
		$return = array();
		if (is_array($msg)) {
			$return = $msg;
		} else {
			$return['msg'] = $msg;
		}
		if ($code!==null) {
			$return['success'] = $code;
		}
		die(json_encode($return));
	}
}